<?php

namespace App\Services;
/**
 * Class KeywordsHistory
 * @package App\Services
 * 2021/3/18 - 10:53
 * Administrator
 * 历史记录
 */
class KeywordsHistory extends BaseService
{
    public $HistoryModel;

    public function __construct()
    {
        $this->HistoryModel = new \App\Models\KeywordsHistory();
    }


    /**
     * 2021/3/18--11:33
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:addHistory
     * @effect:添加用户搜索历史
     */
    public function addHistory($keyword, $user_id)
    {
        $is_exit = $this->HistoryModel->where('search_keyword', $keyword)->where('user_id', $user_id)->first();
        if ($is_exit) {
            $is_exit->search_num = $is_exit->search_num + 1;
            $is_exit->updated_at = now()->timestamp;
            $is_exit->save();
            return $this->format([]);
        }
        $insert = [
            'search_keyword' => $keyword,
            'search_num' => 1,
            'user_id' => $user_id,
            'created_at' => now()->timestamp
        ];
        $this->HistoryModel->insert($insert);
        return $this->format([]);
    }


    public function getModel()
    {
        return $this->HistoryModel;
    }

    /**
     * 析构
     */
    public function __destruct()
    {
        $this->HistoryModel;
    }


}
